//logs.js
const util = require('../../utils/util.js')

Page({
    data: {
        phone: '',
        confirm: '',
        password: '',
        canSend: true,
        inputTimeout: null,
        delay: 1000,
        i: 60,
        timer: null,
        sendText: '点击发送短信'

    },
    phoneInput(e) {
        this.setData({
            phone: e.detail.value
        })
        if (this.data.delay > 0) {
            if (this.inputTimeout) {
                clearTimeout(this.inputTimeout);
            }
            this.inputTimeout = setTimeout(() => {
                if (!(/^1[34578]\d{9}$/.test(this.data.phone))) {
                    this.showToast('手机号格式不对');
                } else {
                    this.setData({
                        canSend: false
                    })
                    console.log(this.data.canSend)
                }
            }, this.data.delay)
        } else {}
    },
    confrimInput(e) {
        this.setData({
            confirm: e.detail.value
        })
    },
    passwordInput(e) {
        this.setData({
            password: e.detail.value
        })
    },

    showToast(message) {
        wx.showToast({
            title: message,
            icon: 'fail',
            duration: 2000,
        })
    },
    sendMsg() {
        wx.request({
            url: 'https://easy-mock.com/mock/5b2385e3debe3c5977248a16/wscn/captcha',
            method: 'GET',
            dataType: 'json',
            success: (res) => {
                if (res.data.code == '200') {
                    this.setData({
                        confirm: res.data.data.captcha
                    })
                    console.log(this.confirm)
                    this.showToast('发送成功');
                }
            },
            fail: ((error) => {
                this.showToast(error.error);
            })
        })
        if (!this.data.canSend) {
            this.data.timer = setInterval(() => {
                this.data.i--;
                this.setData({
                    sendText: `重发验证码${this.data.i}s`,
                    canSend: true
                })
                if (this.data.i == 0) {
                    this.setData({
                        i: 60,
                        canSend: false,
                        sendText: '重新发送'
                    })
                    clearInterval(this.data.timer);
                    return;
                }
            }, 1000)
        }
    },
    login() {
        wx.request({
            url: 'https://easy-mock.com/mock/5b2385e3debe3c5977248a16/wscn/submit',
            method: 'post',
            data: {
                phone: this.data.phone,
                captcha: this.data.confirm
            },
            success: ((res) => {
                if (res.data.code == '200') {
                    this.showToast('登录成功!');
                } else {
                    this.showToast('登录失败!');
                }
            }),
            fail: ((error) => {
                this.showToast(error.error);
            })
        })
    },
    onLoad: function() {
        this.setData({
            logs: (wx.getStorageSync('logs') || []).map(log => {
                return util.formatTime(new Date(log))
            })
        })
    }
})